package com.hue.rabbitmq;

import com.hue.domain.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;

/*
    消息发送者
 */
@Component
public class MsgSender {
    @Autowired
    private AmqpTemplate amqpTemplate;

    //发送消息
    public void sendHello(){
        String hello = "hello";
        System.out.println(new Date().toString()+"发送了一个："+hello);
        //将消息发送到名为hello的消息队列中
        amqpTemplate.convertAndSend("hello",hello);
    }

    //发送对象
    public void sendObj(){
        User user = new User("张三", "123");
        amqpTemplate.convertAndSend("object",user);
    }

    //往一个交换器上发送消息
    public void sendTopic(){
        String hello = "消息被两个队列接收";
        //将消息发送到名为hello的消息队列中
        amqpTemplate.convertAndSend("topicExchange","topic.message",hello);
    }

    //往一个交换器上发送消息
    public void sendTopic1(){
        String hello = "消息被1个队列接收";
        //将消息发送到名为hello的消息队列中
        amqpTemplate.convertAndSend("topicExchange","topic.messages",hello);
    }
}
